//
//  PixelWorldChatModifier.swift
//  demo_swift
//
//  Created by kadenJack on 2022/7/15.
//

import Foundation
import UIKit

//TODO: - 消息源类型
enum PixelWorldChatSourceType: String {
    case none           = ""
    /// 发送消息方
    case sender         = "sender"
    /// 接受消息方
    case receive        = "receive"
}

//TODO: - 消息体类型
enum PixelWorldChatMessageBodyType: String {
    case none                       = ""
    /// 纯文本消息
    case text                       = "MESSAGEBODYTYPE_TEXT"
    /// 礼物消息
    case emoji                      = "MESSAGEBODYTYPE_EMOJI"
    /// 视频消息
    case video                      = "MESSAGEBODYTYPE_VIDEO"
    /// 图片消息
    case image                      = "MESSAGEBODYTYPE_IMAGE"
}

//TODO: - 消息类型
enum PixelWorldChatMessageType {
    case chat, group
}

//TODO: - 消息体模型
import KakaJSON
struct PixelWorldChatMessageBodyModel: Convertible {
    /// 消息源类型
    var sourceType: PixelWorldChatSourceType                       = .none
    var sourceTypeStr: String                                  = ""
    /// 消息类型
    var messageBodyType: PixelWorldChatMessageBodyType             = .none
    var messageBodyTypeStr: String                             = ""
    /// 消息类型
    var messageType: PixelWorldChatMessageType                  = .chat
    /// 消息ID
    var mesageID: String                                        = ""
    /// 用户ID
    var userID: String                                          = ""
    /// 用户环信(会话)ID
    var easeUserID: String                                      = ""
    /// 未读消息数量
    var unReadCount: Int                                        = 0
    /// 最后更新时间
    var lastTime: String?
    /// 最后更新时间
    var lastTimestamp: Int                                      = 0
    /// 最后消息内容
    var lastMessage: String?
    /// 图片消息内容(远程)
    var imageUrl: String?
    /// 图片(只存在于在消息页面时，暂存)
    var image: UIImage?
    /// 对方用户信息
    var userInfo: Any?
    /// 视频渠道消息
    var videoChannelName: String                                = ""
    /// 备注消息
    var remarks: String                                         = ""
}

//TODO: - 发送消息体
struct PixelWorldChatSendMessageBody {
    /// 是否发送CMD消息
    var isSendCmdMessage: Bool                  = false
    /// 消息类型
    var messageBody: PixelWorldChatMessageBodyType = .none
    /// 会话ID
    var conversationID: String                  = ""
    /// 发送方用户唯一标识
    var sender_identifies: String               = ""
    /// 接收方用户唯一标识
    var receive_identifies: String              = ""
    /// 消息内容（纯文本消息、礼物消息、视频通话状态消息）
    var message: String                         = ""
    /// 图片消息
    var image: UIImage?
    /// 发送视频邀请时的渠道名
    var channelName: String                     = ""
    /// 备注消息
    var remarks: String                         = ""
}

//TODO: - 环信用户消息
struct PixelWorldChatUserInfo {
    /// 用户ID(更新用户消息时，可不传)
    var userID: String          = ""
    /// 邮箱
    var email: String?
    /// 联系方式
    var phone: String?
    /// 用户签名
    var sign: String?
    /// 用户昵称
    var nickName: String?
    /// 头像
    var avatar: String?
    /// 性别
    var gender: Int?
    /// 生日
    var birthday: String?
    /// 扩展消息
    var expand: String?
}


//TODO: - 状态返回
enum PixelWorldChatStatusResult {
    /// 成功回调
    case success(_ data: Any?)
    /// 失败消息回调
    case failed(_ error: PixelWorldChatStatusResultError)
    /// 消息列表
    case messages(_ messages: [PixelWorldChatMessageBodyModel])
    
    var messageModels: [PixelWorldChatMessageBodyModel] {
        switch self {
        case .messages(let models):
            return models
        default:
            return []
        }
    }
    
    var isSuccess: Bool {
        switch self {
        case .failed:
            return false
        default:
            return true
        }
    }
    
    var data: Any? {
        switch self {
        case .success(let data):
            return data
        default:
            return nil
        }
    }
    
    var error: PixelWorldChatStatusResultError? {
        switch self {
        case .failed(let err):
            return err
        default:
            return nil
        }
    }
}

//TODO: - 错误信息
enum PixelWorldChatStatusResultError {
    
    case none
    /// 环信错误消息
    case chat(code: Int, msg: String)
    /// 用户未登录
    case isLoggedIn
    /// 参数缺失
    case missingParameter
    /// 消息类型不能为 none
    case message_bodyType
    /// 发送透传消息时不能发送图片
    case message_cmd_image
    /// 不能给自己发消息
    case message_toMe
    /// 发送图片消息时, 图片不能为空
    case message_image
    /// 发送文本消息时,  文本不能为空
    case message_text
    /// 会话ID不能为空
    case message_conversationID
    
    var message: String {
        switch self {
        case .missingParameter:
            return "参数缺失"
        case .isLoggedIn:
            return "环信用户未登录！"
        case .message_bodyType:
            return "发送消息类型不能为 none!"
        case .message_cmd_image:
            return "发送透传消息时, 不能发送图片消息！"
        case .message_toMe:
            return "不能给自己发送消息!"
        case .message_image:
            return "发送图片消息时, 图片不能为空！"
        case .message_text:
            return "发送文本消息时, 文本不能为空！"
        case .message_conversationID:
            return "会话ID不能为空！"
        case .chat(_, let msg):
            return msg
        default:
            return ""
        }
    }
    
    var code: Int {
        switch self {
        case .chat(let code, _):
            return code
        case .missingParameter:
            return 9500300
        case .isLoggedIn:
            return 9500200
        case .message_bodyType:
            return 9500400
        case .message_cmd_image:
            return 9500401
        case .message_toMe:
            return 9500402
        case .message_image:
            return 9500403
        case .message_text:
            return 9500404
        case .message_conversationID:
            return 9500405
        default:
            return 0
        }
    }
}

//TODO: - block 筛选头
enum PixelWorldChatBlockPrefix {
    case autoLogin, newMessage, cmdMessage, conversation, unreadCount, videoStatus, groupMemberChange
    
    var key: String {
        switch self {
        case .autoLogin:
            return "PixelWorldChat_AUTOLOGIN"
        case .newMessage:
            return "PixelWorldChat_NEWMESSAGE"
        case .cmdMessage:
            return "PixelWorldChat_CMDMESSAGE"
        case .conversation:
            return "PixelWorldChat_CONVERSATION"
        case .unreadCount:
            return "PixelWorldChat_UNREADCOUNT"
        case .videoStatus:
            return "PixelWorldChat_VIDEOSTATUS"
        case .groupMemberChange:
            return "PixelWorldChat_GROUPMEMBERCHANGE"
        }
    }
}
